<template>
  <div class="login-container">
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="16" :xl="16">
        <div style="color: transparent">占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left">
          <div class="title-container">
            <h3 class="title">欢迎使用媒资剪辑系统</h3>
          </div>
          <el-form-item prop="username">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="user" />
            </span>
            <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="输入用户名" />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input :type="passwordType" v-model="loginForm.password" name="password" auto-complete="on" placeholder="输入密码" @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon v-show="passwordType == 'password'" icon-class="eye" />
              <svg-icon v-show="passwordType == ''" icon-class="eye_view" />
            </span>
          </el-form-item>
          <!-- <el-form-item prop="code">
        <div style="display:flex;">
          <el-input v-model="loginForm.validCode" name="" auto-complete="on" placeholder="验证码"/>
          <span style="width:100px;height:50px;display:inline-block;" @click="getcode">
            <img :src="code_url" style="width:100%;height:100%;display:inline-block;" alt="">

          </span>
        </div>
      </el-form-item> -->

          <el-button
            :loading="loading"
            class="login-btn"
            type="primary"
            style="width:100%;margin-bottom:30px;"
            @click.native.prevent="handleLogin">
            登录
          </el-button>

          <div style="position:relative">
            <div class="tips">
              <!-- <span> 超级管理员用户名: admin123</span>
          <span> 超级管理员用户名：admin123</span> -->
            </div>
          </div>
        </el-form>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { getcode } from '../../api/login'
// import UUID from 'uuid'
import { uuid } from '../../utils/uuid'
export default {
  name: 'Login',
  data() {
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 6) {
    //     callback(new Error('管理员密码长度应大于6'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      id: '',
      code_url: '',
      loginForm: {
        username: '',
        password: '',
        // validCode: '',
        // deviceId: '',
        grant_type: 'password_code'
      },
      loginRules: {
        username: [{ required: true, message: '账户不允许为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不允许为空', trigger: 'blur' }
          // { validator: validatePassword, trigger: 'blur' }
        ],
        validCode: [
          { required: true, message: '验证码不允许为空', trigger: 'blur' }

        ]
      },
      passwordType: 'password',
      loading: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }

  },
  created() {
    // this.getcode()
    // console.log(UUID)
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    // 获取验证码
    getcode() {
      this.id = uuid()
      getcode(this.id).then(res => {
        // alert(1)
        this.code_url = `http://vtest.cbct.cn/zgc/api-uaa/validata/code/${this.id}`
      }).catch(err => { console.log(err) })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          // this.$set(this.loginForm, 'deviceId', this.id)
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(response => {
            // this.$notify.error({
            //   title: '失败',
            //   message: (response.data || {})['errmsg'] || '请联系系统管理员'
            // })
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 60px;
    width: 100%;
    input {
      background: #f7f6fa;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 45px;
      // color: $light_gray;
      height: 60px;
      caret-color: $base-font-color;
      font-size: $base-font-size-default;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #f7f6fa inset !important;
        // -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        // -webkit-text-fill-color: #fff !important;
      }
    }
  }
  // .el-form-item {
  //   border: 1px solid rgba(255, 255, 255, 0.1);
  //   background: rgba(0, 0, 0, 0.1);
  //   border-radius: 5px;
  //   color: #454545;
  // }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $light_gray;
  background:url('../../assets/login-images/background.jpg') center center fixed no-repeat;
  background-size: cover;
  .login-form {
    position: relative;
    max-width: 100%;
    margin: calc((100vh - 425px) / 2) 10% 10%;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 30px;
      font-weight: 400;
      // color: bl;
      margin: 0px auto 50px auto;
      text-align: center;
      font-weight: bold;
      span{
        font-size: 15px;
        margin-left: 20px;
      }
    }
  }
  .svg-container {
      position: absolute;
      top: 50%;
      transform: translate(0,-50%);
      left: 15px;
      z-index: $base-z-index;
      font-size: 16px;
      color: #979fa5;
      cursor: pointer;
      user-select: none;
    }

    .show-pwd {
      position: absolute;
      top: 50%;
      transform: translate(0,-50%);
      right: 25px;
      font-size: 16px;
      color: #979fa5;
      cursor: pointer;
      user-select: none;
    }
    .login-btn {
      display: inherit;
      width: 220px;
      height: 60px;
      margin-top: 5px;
      border: 0;
      background-color: $base-color-default;
      &:hover {
        opacity: 0.9;
      }
    }
}

</style>
